package net.hvacapp.cmvrfforandroid.ui;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_view_detail)
public class ViewDetailActivity extends BaseActivity {

    @ViewById(R.id.tv_title)
    TextView mTitleTv;
    @ViewById(R.id.tv_info)
    TextView mInfoTv;
    @ViewById(R.id.tv_content)
    TextView mContentTv;
    @ViewById(R.id.iv_image)
    ImageView mImageIv;

    private Dialog mProgressDlg;

    @ViewById(R.id.iv_contract_pic)
    ImageView mContractPicIv; // 合同大图

    private String mImgUrl;

    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
    }

    @Override
    public void initBoot() {

    }

    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
    @Override
    public void initData(Intent intent) {
        String title = intent.getStringExtra("title");
        String info = intent.getStringExtra("info");
        String content = intent.getStringExtra("content");
        String guidId = intent.getStringExtra("id");
//        String picUrl;
        int type = intent.getIntExtra("type", 0);
        if (type == 0) {
            setupToolbar(0, "公告查看", null);
            mImgUrl = HttpConstant.HTTP_URL + "2.0/postmanager.svc/viewpic?tk=" + AccountManager.getInstance().getToken() + "&id=" + guidId;
        }
        else {
            setupToolbar(0, "意见查看", null);
            mImgUrl = HttpConstant.HTTP_URL + "2.0/suggestionmanager.svc/viewpic?tk=" + AccountManager.getInstance().getToken() + "&id=" + guidId;
        }

        mTitleTv.setText(title);
        mInfoTv.setText(info);
        mContentTv.setText(content);

        mProgressDlg = createProgressBarDialog(this, "图片加载中...");
        ImageLoader.getInstance().displayImage(mImgUrl, mImageIv, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                mProgressDlg.show();
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                mProgressDlg.dismiss();
                mImageIv.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                mProgressDlg.dismiss();
                if (loadedImage == null) {
                    mImageIv.setVisibility(View.GONE);
                }
                else {
                    mImageIv.setVisibility(View.VISIBLE);
                    mContractPicIv.setImageBitmap(loadedImage);
                }
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
        });

        initEvent();
    }

    private void initEvent() {
        mImageIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mContractPicIv.getVisibility() == View.GONE) {
                    mContractPicIv.setVisibility(View.VISIBLE);
                }
//                Intent it =new Intent(Intent.ACTION_VIEW);
//                it.setDataAndType(Uri.parse(mImgUrl), "image/*");
//                startActivity(it);
            }
        });
        // 关闭合同原图
        mContractPicIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mContractPicIv.getVisibility() == View.VISIBLE) {
                    mContractPicIv.setVisibility(View.GONE);
                }
            }
        });
    }


    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }


}
